import produce from 'immer';
import storage from 'zhihu-utilities/lib/storage';
export const dmp2_selectedGroupTag_key = 'dmp2:selectedGroupTag';

const mapTagGroupIds = {
  340004: 1170004,
  1330001: 750008,
};

export const updateGroupIdStorage = () => {
  const storageData = storage.get(dmp2_selectedGroupTag_key);
  if (!storageData) return;
  const nextStorage = storageData.map((tagItem) => {
    const nextGroupId = mapTagGroupIds[tagItem.groupId];
    if (nextGroupId) {
      return produce(tagItem, (draft) => {
        draft.groupId = nextGroupId;
        draft.groupTagInfo.groupId = nextGroupId;
      });
    }
    return tagItem;
  });
  storage.set(dmp2_selectedGroupTag_key, nextStorage);
};

export const getSelectedTags = () => {
  return storage.get(dmp2_selectedGroupTag_key);
};

export const setSelectedTags = (selectedTags) => {
  return storage.set(dmp2_selectedGroupTag_key, selectedTags);
};

export const deleteSelectedTags = () => {
  return storage.delete(dmp2_selectedGroupTag_key);
};
